package biz.almas.uycnr.crawler.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 解析xml时用的验证类，
 * @Copyright: Copyright (c) 2015 XinJiang Almas Software All Rights Reserved
 * @Company: XinJiang Almas Software
 * @author Ansar786 Nov 23, 2015
 * @version V1.0
 * @history:
 * 
 */
public class CnrValidator {

	public final static String DATETIME_YMDHMS = "yyyy-MM-dd HH:mm:ss";

	public static final boolean isNull(String value) {
		return value == null || value.trim().length() == 0;
	}

	/**
	 * 一般用于解析【写】操作的参数，如果为空或非法，抛出异常
	 */
	public static final String parseString(String value, String exception)
			throws ParseException {
		isNull(value, exception);
		return value;
	}

	/**
	 * 一般用于解析【写】操作的参数(但不需要返回值），如果为空或非法，抛出异常
	 */
	public static final void isNull(String value, String exception)
			throws ParseException {
		if (isNull(value)) {
			throw new ParseException(exception, 0);
		}
	}

	/**
	 * parse string to integer Ansar786 Nov 23, 2015
	 * 
	 * @param value
	 * @return
	 */
	public static int parseInt(String value) {
		try {
			return Integer.parseInt(value);
		} catch (NumberFormatException e) {
			return 0;
		}
	}

	public static final int parseInt(String value, String exception)
			throws ParseException {
		try {
			return Integer.parseInt(value);
		} catch (NumberFormatException e) {
			throw new ParseException(exception, 0);
		}
	}

	/**
	 * 字符型日期时间转换成日期型
	 * 
	 * @param ref_dt
	 * @return
	 */
	public static Date string2DateTime(String ref_dt) {
		if (isNull(ref_dt)) {
			return null;
		}
		try {
			return new SimpleDateFormat(DATETIME_YMDHMS).parse(ref_dt);
		} catch (ParseException pe) {
			return null;
		}
	}

	public static void main(String[] args) {
		Map<String, String> map = new HashMap<String, String>();
		System.out.println(map.get("title"));
	}
}
